package algorithm_learn.random;

public class Code_RangeSum2_mian {
    public static void main(String[] args) {
        Code_RangeSum2 co = new Code_RangeSum2();
        int[] a = {8,4,6,3,5,2,1,1,3,4,1};
        co.RangeSum2(a);
        System.out.println("sum: "+co.rangeSum(6,9));
    }
}

class Code_RangeSum2 {
     public int[] preSum;
    //返回前缀和数组
    public void RangeSum2(int[] array){
        int N = array.length;
        preSum = new int[N];
        preSum[0] = array[0];
        for (int i=1;i<N;i++){
            preSum[i] = preSum[i-1] + array[i];
        }
    }
    //区间加法
    public int rangeSum(int l,int r){
        return l == 0 ? preSum[r] : preSum[r] - preSum[l-1];
    }
}